import 'dart:convert';

import 'package:core/core.dart';
import 'package:dio/dio.dart';

///日志拦截器
class DioLogInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    String requestStr = "==================== REQUEST START ====================\n"
        "- URL:${options.baseUrl + options.path}\n"
        "- METHOD: ${options.method}\n";
    requestStr += "- HEADER:${jsonEncode(options.headers)}\n";

    final params = options.queryParameters;
    if(params.isNotEmpty){
      requestStr += '- PARAMS:${jsonEncode(params)}\n';
    }

    final data = options.data;
    if (data != null) {
      if (data is Map) {
        requestStr += "- BODY:\n${jsonEncode(data)}\n";
      } else if (data is FormData) {
        final formDataMap = {}
          ..addEntries(data.fields)
          ..addEntries(data.files);
        requestStr += "- BODY:\n${jsonEncode(formDataMap)}\n";
      } else {
        requestStr += "- BODY:\n${data.toString()}\n";
      }
    }
    requestStr += "==================== REQUEST END ====================";
    LogHelper.d(requestStr);
    handler.next(options);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    ToastHelper.closeLoading();
    String errorStr = "==================== DioException START ====================\n"
        "- URL:\n${err.requestOptions.baseUrl + err.requestOptions.path}\n"
        "- METHOD: ${err.requestOptions.method}\n";

    errorStr += "- HEADER:\n${jsonEncode(err.requestOptions.headers)}\n";
    if (err.response != null && err.response?.data != null) {
      errorStr += "- ERROR:\n${jsonEncode(err.response?.data)}\n";
    } else {
      errorStr += "- ERRORTYPE: ${err.type}\n";
      errorStr += "- MSG: ${err.message}\n";
    }
    errorStr += "==================== DioException END ====================";
    LogHelper.d(errorStr);
    handler.next(err);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    String responseStr = "==================== RESPONSE START ====================\n"
        "- URL:\n${response.requestOptions.uri}\n";
    // responseStr += "- HEADER:\n{";
    // response.headers.forEach((key, list) => responseStr += "\n  \"$key\" : \"$list\",");
    // responseStr += "\n}\n";
    // responseStr += "- STATUS: ${response.statusCode}\n";

    if (response.data != null) {
      responseStr += "- BODY:\n ${jsonEncode(response.data)}\n";
    }
    responseStr += "==================== RESPONSE END ====================";
    LogHelper.d(responseStr);
    handler.next(response);
  }

  String _parseResponse(Response? response) {
    String responseStr = "";
    var data = response?.data;
    if (data is Map) {
      responseStr += data.mapToStructureString();
    } else if (data is List) {
      responseStr += data.listToStructureString();
    } else {
      responseStr += response?.data?.toString() ?? '';
    }

    return responseStr;
  }
}
